package org.muses.ssm.senior.mgt.demo.controller;

import org.muses.ssm.senior.mgt.demo.entity.Employee;
import org.muses.ssm.senior.mgt.demo.entity.MusesResult;
import org.muses.ssm.senior.mgt.demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 列表页面
     *
     * @param employee
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(Employee employee) {
        ModelAndView mv = new ModelAndView("index");
        PageInfo<Employee> list = employeeService.list(employee);
        mv.addObject("pages", list);
        return mv;
    }

    /**
     * 添加页面
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("add");
        return mv;
    }

    /**
     * 编辑页面
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit/{id}")
    public ModelAndView edit(@PathVariable(name = "id", required = false) Integer id) {
        Employee employee = employeeService.getEmpById(id);
        ModelAndView mv = new ModelAndView("update");
        mv.addObject("employee", employee);
        return mv;
    }

    /**
     * 添加
     *
     * @param employee
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public MusesResult add(@RequestBody Employee employee) {
        return employeeService.add(employee);
    }

    /**
     * 编辑
     *
     * @param employee
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public MusesResult update(@RequestBody Employee employee) {
        return employeeService.update(employee);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public MusesResult update(@PathVariable(value = "id") Integer id) {
        Employee employee = employeeService.getEmpById(id);
        return MusesResult.success("", employee);
    }

}
